package org.dubbo.live.common.interfaces.controller;

import jakarta.annotation.Resource;
import org.dubbo.live.common.interfaces.service.ILivingRoomServer;
import org.dubbo.live.common.interfaces.vo.LivingRoomInitVO;
import org.dubbo.live.common.interfaces.vo.WebResponseVO;
import org.dubbo.live.common.interfaces.vo.req.LivingRoomReqVO;
import org.qiyu.live.web.starter.config.RequestLimit;
import org.qiyu.live.web.starter.context.QiyuRequestContext;
import org.qiyu.live.web.starter.error.BizBaseErrorEnum;
import org.qiyu.live.web.starter.error.ErrorAssert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 开关播
 * @Author: 张广焱
 * @Date: 2024-05-17 下午9:43
 */
@RestController
@RequestMapping("/living")
public class LivingRoomController {
    @Resource
    private ILivingRoomServer livingRoomServer;

    @PostMapping("/list")
    public WebResponseVO list(LivingRoomReqVO livingRoomReqVO) {
        ErrorAssert.isTure(livingRoomReqVO != null && livingRoomReqVO.getType() != null, BizBaseErrorEnum.PARAM_ERROR);
        ErrorAssert.isTure(livingRoomReqVO.getPage() > 0 && livingRoomReqVO.getPageSize() <= 100, BizBaseErrorEnum.PARAM_ERROR);
        return WebResponseVO.success(livingRoomServer.list(livingRoomReqVO));
    }

    /**
     * @Description:开播
     * @Author: 张广焱
     * @Date: 2024-05-18  13:20:16
     */
    @RequestLimit(limit = 1, second = 10, msg = "开播请求过于频繁，请稍后再试")
    @PostMapping("/startingLiving")
    public WebResponseVO startingLiving(Integer type) {
        //调用rpc，往我们的开播表t_living_room写入一条数据
        ErrorAssert.isNotNull(type, BizBaseErrorEnum.PARAM_ERROR);
        Integer roomId = livingRoomServer.startingLiving(type);
        LivingRoomInitVO livingRoomInitVO = new LivingRoomInitVO();
        livingRoomInitVO.setRoomId(roomId);
        return WebResponseVO.success(livingRoomInitVO);

    }

    /**
     * @Description: 关播
     * @Author: 张广焱
     * @Date: 2024-05-18  13:20:24
     */
    @RequestLimit(limit = 1, second = 10, msg = "关播请求过于频繁，请稍后再试")
    @PostMapping("/closeLiving")
    public WebResponseVO closeLiving(Integer roomId) {
        ErrorAssert.isNotNull(roomId, BizBaseErrorEnum.PARAM_ERROR);
        boolean closeStatus = livingRoomServer.closeLiving(roomId);
        if (closeStatus) {
            return WebResponseVO.success();
        }
        return WebResponseVO.bizError("关播异常");
    }

    /**
     * @Description: 获取主播相关配置信息
     * @Author: 张广焱
     * @Date: 2024-05-18  13:20:33
     */
    @PostMapping("/anchorConfig")
    public WebResponseVO anchorConfig(Integer roomId) {
        return WebResponseVO.success(livingRoomServer.anchorConfig(QiyuRequestContext.getUserId(), roomId));
    }
}
